package cn.area.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import cn.area.domain.TripInfo;
import cn.area.global.MapConfig;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;

public class JourneyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	ArrayList<GeoPoint> geoPointList;
	Projection projection;


	Point point = null;
	GeoPoint peoplePoint = null;

	Bitmap bitmap = null;

	public JourneyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		populate(); // createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	public void clear() {
		mOverlays.clear();
		populate();
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public JourneyItemizedOverlay(Drawable defaultMarker, Context context) {
		this(defaultMarker);
		this.bitmap = MapConfig.bitmapPeople;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		MapConfig.journeyMapView = mapView;
		MapConfig.pointList = new ArrayList<Point>();
		if (MapConfig.infos != null && MapConfig.infos.size() > 1) {
			projection = mapView.getProjection();
			geoPointList = new ArrayList<GeoPoint>();
			for (TripInfo tripInfo : MapConfig.infos) {
				Point p = new Point();
				MapConfig.pointList.add(p);
				GeoPoint geopoint = new GeoPoint(
						(int) (tripInfo.getLat() * 1e6),
						(int) (tripInfo.getLng() * 1e6));
				geoPointList.add(geopoint);
			}
			// 经度转像素
			int i = 0;
			for (GeoPoint gp : geoPointList) {
				projection.toPixels(gp, MapConfig.pointList.get(i));
				i++;
			}

			// 第一个画笔 画圆(画每个点的圆)
			Paint fillPaint = new Paint();
			fillPaint.setColor(Color.BLUE);
			fillPaint.setAntiAlias(true);
			fillPaint.setStyle(Style.FILL);

			// 将图画到上层
			for (Point p : MapConfig.pointList) {
				canvas.drawCircle(p.x, p.y, 5.0f, fillPaint);
			}

			// 第二个画笔 画线
			Paint paint = new Paint();
			paint.setColor(Color.BLUE);
			paint.setDither(true);
			paint.setStyle(Style.STROKE);
			paint.setStrokeJoin(Paint.Join.ROUND);
			paint.setStrokeCap(Paint.Cap.ROUND);
			paint.setStrokeWidth(4);

			// 连接
			Path path = new Path();
			path.moveTo(MapConfig.pointList.get(0).x,
					MapConfig.pointList.get(0).y);

			for (int j = 1; j < MapConfig.pointList.size(); j++) {
				path.lineTo(MapConfig.pointList.get(j).x,
						MapConfig.pointList.get(j).y);
			}
			// 画出路径
			canvas.drawPath(path, paint);
		}

	}
	
	

	// 弹出的图片框
	protected boolean onTap(int i) {
		setFocus(mOverlays.get(i));
		if (MapConfig.infos.size() > 0) {
			GeoPoint pt = mOverlays.get(i).getPoint();
			
			MapConfig.journeyMapView.updateViewLayout(MapConfig.popupView,
					new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT, pt,
							MapView.LayoutParams.BOTTOM_CENTER));
			MapConfig.journeyMapView.updateViewLayout(MapConfig.popPeopleView,
					new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT, pt,
							MapView.LayoutParams.BOTTOM_CENTER));
			
			MapConfig.popupView.setVisibility(View.VISIBLE);
			MapConfig.popPeopleView.setVisibility(View.VISIBLE);
			
		}
		return true;
	}
	
	
	
}
